package cn.kent.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitConfig {
    public static final String QUEUE_NAME = "hello_queue_name";
    public static final String EXCHANGE_NAME = "hello_exchange_name";
    public static final String DELAY_QUEUE_NAME = "delay_queue_name";
    public static final String DELAY_EXCHANGE_NAME = "delay_exchange_name";


    @Bean
    Queue msgQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 10000);
        args.put("x-dead-letter-exchange", DELAY_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", DELAY_QUEUE_NAME);
        return new Queue(QUEUE_NAME, true, false, false, args);
    }

    @Bean
    DirectExchange msgExchange() {
        return new DirectExchange(EXCHANGE_NAME, true, false);
    }

    @Bean
    Binding msgBinding() {
        return BindingBuilder.bind(msgQueue())
                .to(msgExchange())
                .with(QUEUE_NAME);
    }


    @Bean
    Queue dlxQueue() {
        return new Queue(DELAY_QUEUE_NAME, true, false, false);
    }

    @Bean
    DirectExchange dlxExchange() {
        return new DirectExchange(DELAY_EXCHANGE_NAME, true, false);
    }

    @Bean
    Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue())
                .to(dlxExchange())
                .with(DELAY_QUEUE_NAME);
    }
}
